//
//  SecondViewController.m
//  MyAppDemo
//
//  Created by 千锋 on 15/12/5.
//  Copyright (c) 2015年 Aike. All rights reserved.
//

#import "SecondViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface SecondViewController ()<AVAudioPlayerDelegate>
{
    NSURL * _musicURL;
    NSArray * _musicArray;
}
- (IBAction)beginClicked:(UIButton *)sender;
- (IBAction)stopClicked:(UIButton *)sender;
- (IBAction)nextMusicClicked:(UIButton *)sender;
- (IBAction)handerMusicClicked:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *musicName;
@property (weak, nonatomic) IBOutlet UILabel *musicWords;
@property(nonatomic,copy)AVAudioPlayer * player;

@end

@implementation SecondViewController
static int _current = 0;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.title = @"啧啧";
    //self.view.backgroundColor = [UIColor orangeColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)beginClicked:(UIButton *)sender {
    _musicURL = [[NSBundle mainBundle]URLForResource:_musicArray[_current] withExtension:@"mp3"];
    AVAsset * musicAsset = [AVAsset assetWithURL:_musicURL];
    NSArray * id3 = [musicAsset metadataForFormat:@"org.id3"];
    for (AVMetadataItem * item in id3) {
        //NSLog(@"%@",item.key,item.value);
        NSString * keyString = [NSString stringWithFormat:@"%@",item.key];
        NSString * valueString = [NSString stringWithFormat:@"%@",item.value];
        if ([keyString isEqualToString:@"TIT2"]) {
            _musicName.text = item.stringValue;
        }if ([valueString isEqualToString:@"TPE1"]) {
            _musicWords.text = item.stringValue;
        }
    }
    NSError * error = nil;
    _player = [[AVAudioPlayer alloc]initWithContentsOfURL:_musicURL error:&error];
    _player.volume = 1.0;
    _player.delegate = self;
    BOOL isPrepare = [_player prepareToPlay];
    if (isPrepare) {
        [_player play];
    }
    
    

}

- (IBAction)stopClicked:(UIButton *)sender {
    [_player stop];
}

- (IBAction)nextMusicClicked:(UIButton *)sender {
    _musicArray = @[@"Beat It",@"Poker Face",@"像梦一样自由",@"北京北京",@"春天里",@"江南",@"狐狸叫",@"蓝莲花"];
    
    _current++;
    _musicURL = [[NSBundle mainBundle]URLForResource:_musicArray[_current] withExtension:@"mp3"];
    AVAsset * musicAsset = [AVAsset assetWithURL:_musicURL];
    NSArray * id3 = [musicAsset metadataForFormat:@"org.id3"];
    for (AVMetadataItem * item in id3) {
        //NSLog(@"%@",item.key,item.value);
        NSString * keyString = [NSString stringWithFormat:@"%@",item.key];
        NSString * valueString = [NSString stringWithFormat:@"%@",item.value];
        if ([keyString isEqualToString:@"TIT2"]) {
            _musicName.text = item.stringValue;
        }if ([valueString isEqualToString:@"TPE1"]) {
            _musicWords.text = item.stringValue;
        }
    }
    //获取屏幕的宽度
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    NSError * error = nil;
    _player = [[AVAudioPlayer alloc]initWithContentsOfURL:_musicURL error:&error];
    _player.volume = 1.0;
    _player.delegate = self;
    BOOL isPrepare = [_player prepareToPlay];
    if (isPrepare) {
        [_player play];
    }
    //  UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 50, screenSize.width, screenSize.height)];
    //    NSMutableArray * images = [[NSMutableArray alloc]init];
    //    for (int x = 2; x <= 8; x++) {
    //        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"bg_0%d",x]];
    //        [images  addObject:image];
    //
    //    }
    //    imageView.image = [UIImage imageNamed:images[_current]];
    //    [self.view addSubview:imageView];
    if (_current == 7) {
        _current = 0;
    }

    
}

- (IBAction)handerMusicClicked:(UIButton *)sender {
    
}

#pragma mark - 代理
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"播放完成");
    
}

-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{
    
}
@end
